package com.qiantu.phone.ui.activity;

import android.text.TextUtils;

import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.entity.SceneDetailBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.adapter.ScenePrivateDetailAdapter;
import com.qiantu.phone.utils.TempDataHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 编辑私人场景
 */
public class EditScenePrivateActivity extends EditSceneBaseActivity {

    private ScenePrivateDetailAdapter mScenePrivateDetailAdapter;

    @Override
    protected void initData() {
        sceneDetailBean = TempDataHolder.getEditSceneDetailBean(4);
        if (sceneDetailBean == null){
            SceneDetailBean addScene = new SceneDetailBean();
            addScene.setBingSceneSerialNo(getString("bingSceneSerialNo"));
            addScene.setHouseSerialNo(AppApplication.getInstance().getCurHouseInfo().getHouseSerialNo());
            addScene.setSceneType(4);
            SceneDetailBean mainSceneDetailBean = TempDataHolder.getEditSceneDetailBean(1);
            if(mainSceneDetailBean != null){
                addScene.setSceneTasks(new ArrayList<>(mainSceneDetailBean.getSceneTasks()));
            }
            addScene.setName(mainSceneDetailBean.getName());
            addScene.setImage(mainSceneDetailBean.getImage());
            addScene.setRoomSerialNo(mainSceneDetailBean.getRoomSerialNo());
            TempDataHolder.setEditSceneDetailBean(addScene);
            sceneDetailBean = addScene;
        }
        initFavourite = false;
        initFavouriteRoomSerialNo = sceneDetailBean.getRoomSerialNo();
        if (sceneDetailBean.getSceneTasks() == null){
            sceneDetailBean.setSceneTasks(new ArrayList<>());
        }
        mScenePrivateDetailAdapter = new ScenePrivateDetailAdapter(this, sceneDetailBean);
        mRecyclerView.setAdapter(mScenePrivateDetailAdapter);
        if (!TextUtils.isEmpty(sceneDetailBean.getSceneSerialNo())){
            isCreate = false;
        }else {
            isCreate = true;
        }
        setTitle(getString(R.string.task_item));
    }

    @Override
    protected boolean submitCheck() {
        List<SceneDetailBean.SceneTaskBean> sceneTasks = sceneDetailBean.getSceneTasks();
        if(sceneTasks == null || sceneTasks.size() == 0){
            toast(R.string.please_add_valid_task);
            return false;
        }
        boolean isAllDelayTask = true;
        for(SceneDetailBean.SceneTaskBean task : sceneTasks){
            if(task.getSceneTaskType() != 2){
                isAllDelayTask = false;
                break;
            }
        }
        if(isAllDelayTask){
            toast(R.string.please_add_valid_task);
            return false;
        }

        return true;
    }

    @Override
    protected RecyclerView.Adapter getAdapter() {
        return mScenePrivateDetailAdapter;
    }
}